package sctu.edu.java.year2016.team01.OurTeamProject.subroutine;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

/**
 * Created by Zero on 2016/6/25.
 */
public class Register {


    public String path(){

        return "src\\main\\java\\sctu\\edu\\java\\year2016\\team01\\OurTeamProject";
    }

    public void setUp() throws Exception{

        Register register=new Register();
        RegisterSuccess a=new RegisterSuccess();
        Read validate=new Read();

        File file=new File("D:/","user.txt");
        if(!file.exists())
        {
            file.createNewFile();
        }

        JFrame f = new JFrame();
        f.setLayout(null);
        f.setTitle("用户注册");
        f.setBounds(700, 350, 700, 400);
        Container con = f.getContentPane();
        JPanel pan = new JPanel();

        ImageIcon background=new ImageIcon(register.path()+"\\背景.png");
        JLabel label=new JLabel(background);
        label.setBounds(0, 0, 700, 400);
        JPanel imagePanel = (JPanel) f.getContentPane();
        imagePanel.setOpaque(false);
        f.getLayeredPane().add(label, new Integer(Integer.MIN_VALUE));




        JLabel accountNumber=new JLabel("账号：");
        accountNumber.setFont(new Font("宋体", Font.BOLD, 21));
        accountNumber.setForeground(Color.white);
        f.getContentPane().add(accountNumber);
        accountNumber.setBounds(100, 120, 80, 30);


        JTextField numberContent=new JTextField(20);
        f.getContentPane().add(numberContent);
        numberContent.setBounds(160, 120, 200, 30);
        numberContent.setForeground(Color.white);
        numberContent.setOpaque(false);






        JLabel password=new JLabel("密码：");
        password.setFont(new Font("宋体", Font.BOLD, 21));
        password.setForeground(Color.white);
        f.getContentPane().add(password);
        password.setBounds(100, 170, 80, 30);



        JTextField passwordContent=new JTextField(20);
        f.getContentPane().add(passwordContent);
        passwordContent.setBounds(160, 170, 200, 30);
        passwordContent.setForeground(Color.white);
        passwordContent.setOpaque(false);


        JButton register2=new JButton("立即注册");
        register2.setFont(new Font("宋体", Font.BOLD, 19));
        register2.setContentAreaFilled(false);
        register2.setForeground(Color.white);
        register2.setBounds(245, 240, 115, 40);
        f.getContentPane().add(register2);




        register2.addActionListener(new ActionListener() {
                                        @Override
                                        public void actionPerformed(ActionEvent e) {

                                            BufferedWriter writer = null;

                                            try {




                                                for (int i = 0; i < validate.read().size(); i++) {
                                                    if (validate.read().get(i).equals(numberContent.getText())) {

                                                        JLabel accountNumber = new JLabel("用户名已存在...");
                                                        accountNumber.setFont(new Font("宋体", Font.BOLD, 21));
                                                        accountNumber.setForeground(Color.white);
                                                        f.getContentPane().add(accountNumber);
                                                        accountNumber.setBounds(400, 240, 200, 40);
                                                        return;
                                                    }
                                                }




                                                FileWriter save = new FileWriter(file, true);
                                                writer = new BufferedWriter(save);
                                                writer.write(numberContent.getText() + "*");
                                                writer.write(passwordContent.getText() + "\r\n");
                                                a.registerSuccess("注册成功...");
                                                f.dispose();



                                            } catch (Exception e1) {
                                                e1.printStackTrace();
                                            } finally {
                                                try {
                                                    writer.close();
                                                } catch (IOException e1) {
                                                    e1.printStackTrace();
                                                }
                                            }
                                        }
                                    }
        );

            f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
            con.add(pan);
            f.setVisible(true);

        }
    }
